<script setup lang="ts">
import {
  Button, Divider, Tag,
} from 'vant';
import type { PropType } from 'vue';
import { computed } from 'vue';

import { ALREADY_HANDLE } from '../const';
import type WithdrowItem from '../models/WithdrowItem';

const props = defineProps({
  record: {
    type: Object as PropType<WithdrowItem>,
    required: true,
  },
});

const emit = defineEmits(['approve']);

const handleApprove = () => {
  if (props.record.status === ALREADY_HANDLE) {
    return;
  }
  emit('approve', props.record);
};

// 状态映射
const statusMap = computed(() => {
  const map: Record<number, { text: string; type: 'primary' | 'success' | 'danger' | 'warning' }> = {
    1: { text: '已完成', type: 'success' },
    2: { text: '待审核', type: 'primary' },
  };
  return map[props.record.status] || { text: `未知状态(${props.record.status})`, type: 'warning' };
});

// 格式化时间
const formatTime = (time: string) => {
  if (!time) return '-';
  return new Date(time).toLocaleString();
};

</script>

<template>
  <div class="with-card">
    <!-- 卡片头部 - 提现单号及状态 -->
    <div class="card-header">
      <div class="order-info">
        <div class="order-id">提现单号:
          {{ record.withdrawalId }}
        </div>
        <div class="flow-id">流水号:
          {{ record.flowId }}
        </div>
      </div>
      <Tag :type="statusMap.type" size="medium">
        {{ statusMap.text }}
      </Tag>
    </div>
    <Divider />
    <!-- 提现信息部分 -->
    <div class="card-section">
      <h3>提现信息</h3>
      <div class="info-grid">
        <div class="info-item">
          <span class="label">用户账户</span>
          <span
            class="value">{{ record.account }}</span>
        </div>

        <div class="info-item">
          <span class="label">提现金额</span>
          <span class="value amount">
            ¥{{
              (record.withdrawalAmount) }}
          </span>
        </div>
        <div class="info-item">
          <span class="label">实际到账</span>
          <span class="value amount">¥{{
            (record.actualAmount)
            }}</span>
        </div>
        <div class="info-item full-width">
          <span class="label">提现账户</span>
          <span
            class="value">{{ (record.bankAccount) }}</span>
        </div>
        <div class="info-item">
          <span class="label">申请人</span>
          <span
            class="value">{{ record.userName }}</span>
        </div>
      </div>
    </div>

    <Divider />

    <!-- 时间信息部分 -->
    <div class="card-section">
      <h3>时间信息</h3>
      <div class="info-grid">
        <div class="info-item">
          <span class="label">申请时间</span>
          <span
            class="value">{{ formatTime(record.createTime) }}</span>
        </div>
        <div class="info-item">
          <span class="label">审核时间</span>
          <span
            class="value">{{ formatTime(record.auditTime) || '-' }}</span>
        </div>
      </div>
    </div>

    <!-- 审批按钮区域 -->
    <div v-if="record.status !== ALREADY_HANDLE"
      class="footer">
      <Button type="primary" size="small"
        :disabled="record.status === ALREADY_HANDLE"
        class="approve-btn"
        @click="handleApprove">审批</Button>
    </div>
  </div>
</template>

<style lang="less" scoped>
.with-card {
  border-radius: 12px;
  background-color: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 16px;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
  color: #333;

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;

    .order-info {
      .order-id {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 4px;
      }

      .flow-id {
        font-size: 13px;
        color: #666;
      }
    }
  }

  .card-section {
    margin: 16px 0;

    h3 {
      margin: 0 0 12px;
      font-size: 15px;
      font-weight: 600;
      color: #2c3e50;
      position: relative;
      padding-left: 8px;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 4px;
        height: 16px;
        width: 3px;
        background-color: #1989fa;
        border-radius: 2px;
      }
    }

    .info-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 12px;

      .info-item {
        display: flex;
        flex-direction: column;

        &.full-width {
          grid-column: span 2;
        }

        .label {
          font-size: 13px;
          color: #7d7d7d;
          margin-bottom: 4px;
        }

        .value {
          font-size: 14px;
          font-weight: 500;
          word-break: break-all;

          &.amount {
            color: #ff4d4f;
            font-weight: 600;
            font-size: 15px;
          }
        }
      }
    }
  }

  .footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px dashed #eee;

    .approve-btn {
      min-width: 80px;
      border-radius: 20px;
      font-weight: 500;
    }

    .reject-btn {
      min-width: 80px;
      border-radius: 20px;
      font-weight: 500;
    }
  }
}
</style>
